package com.zzxx.controller;

import com.github.pagehelper.PageInfo;
import com.zzxx.pojo.Product;
import com.zzxx.service.ProductService;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DateConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("procontroller")
public class ProductController {

    @Autowired
    private ProductService productService;

    @RequestMapping("/getid")
    public String getProductById(String id,Model model){
        Product productByPK = productService.findProductByPK(id);
        model.addAttribute("product",productByPK);
        return "/index.jsp";
    }

    @RequestMapping("/list")
    @ResponseBody
    public PageInfo<Product> getProducts(@RequestParam(defaultValue = "1") Integer pageNum,@RequestParam(defaultValue = "2") Integer pageSize){
        PageInfo<Product> pageInfo = productService.getAllProducts(pageNum,pageSize);
        return pageInfo;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public void removeProduct(String[] ids){
        for(int i = 0;i < ids.length;i++){
            System.out.println(ids[i]);
            productService.deleteById(ids[i]);
        }
    }

    @RequestMapping("/add")
    @ResponseBody
    public void add(@RequestBody Product product){
        product.setId(UUID.randomUUID().toString().replace("-",""));
        product.setDepartureTime(new Date());
        System.out.println(product);
        productService.add(product);
    }
}
